package com.xiaohudie.Optional;

import com.xiaohudie.Lambda.Booke;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @Author xiao hu die
 * @Date 2022/2/27 12:57
 * @Version 1.0
 */
public class test01 {
    public static void main(String[] args) {
   /*     Booke booke = new Booke();*/
       /* booke =null;
        System.out.println(booke.getId());*/
       /* Optional<Booke> booke1 = Optional.ofNullable(booke);
        booke1.ifPresent(x-> System.out.println(x.getId()));*/
       /**未用optional*/
       /* List<Booke> booke = getBooke();
        for (Booke booke1 : booke) {
            System.out.println(booke1.getId());
        }*/
        /**使用optional*/
        List<Optional<Booke>> booke = getBooke();
        List<Integer> list =new ArrayList<>();
        for (Optional<Booke> optionalBooke : booke) {
            optionalBooke.ifPresent(x->list.add(x.getId()));
        }
        System.out.println(list);
        List<Booke> booke2 = getBooke2();
        for (Booke booke1 : booke2) {
            System.out.println(booke1.getId());
        }
    }

    public static List<Optional<Booke>> getBooke() {
        List<Optional<Booke>> bookeList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Booke booke = new Booke(String.valueOf(i), i, 9l);
            Optional<Booke> booke1 = Optional.ofNullable(booke);
            bookeList.add(booke1);
        }
        Booke booke = new Booke();
        booke =null;
        Optional<Booke> booke1 = Optional.ofNullable(booke);
        bookeList.add(booke1);
        return bookeList;
    }
    public static List<Booke> getBooke2() {
        List<Booke> bookeList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Booke booke = new Booke(String.valueOf(i), i, 9l);
            bookeList.add(booke);
        }
        Booke booke = new Booke();
        booke =null;
        bookeList.add(booke);
        return bookeList;
    }
}

